<?php

namespace app\site\service\store;

use app\model\store\Coupon;
use app\site\service\traits\InstanceTrait;
use think\db\Query;

class CouponService extends \app\site\service\SiteBaseService
{
    use InstanceTrait;

    function initialize()
    {
        $this->model = new Coupon();
    }

    public function query(): Query
    {
        return $this->makeQuery()->filter(function ($v) {
            if ($v->expire_type == 2){
                $v->expire_date = [miDate('Y-m-d', $v->start_date), date('Y-m-d', $v->end_date)];
            }
            $v->getLevelIdArr = miExplode($v->get_level_ids);
            $v->applyGoodsIdArr = miExplode($v->apply_goods_ids);
        });
    }

    function formatData(array $data): array
    {
        if($data['start_date']??'' && strtotime($data['start_date'])){
            $data['start_date'] = strtotime($data['start_date']);
        }

        if($data['end_date']??'' && strtotime($data['end_date'])){
            $data['end_date'] = strtotime($data['end_date']);
        }

        if ($data['applyGoodsIdArr']??''){
            $data['apply_goods_ids'] = miImplode($data['applyGoodsIdArr']);
        }

        if ($data['getLevelIdArr']??''){
            $data['get_level_ids'] = miImplode($data['getLevelIdArr']);
        }

        return $data;
    }
}
